package com.situ.mall.controller.back;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Result;
import com.situ.mall.entity.User;
import com.situ.mall.service.IUserService;

@RequestMapping("/user")
@Controller
public class UserController {
	
	@Autowired
	private IUserService userService;

	@RequestMapping("/exportExcel.action")
	public void exportExcel(HttpServletResponse response) {
		userService.exportExcel(response);
	}
	@RequestMapping("/getUserListPage.action")
	public String getCategoryListPage() {
		return "user_list";
	}
	
	@RequestMapping("/selectByPage.action")
	@ResponseBody
	public Result selectByPage(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "5") Integer limit) {
		System.out.println("UserController.selectByPage()");
		Result result=userService.selectByPage(page,limit);
		return result;
	} 
	
	@RequestMapping("/deleteAll.action")
	@ResponseBody
	public Result deleteAll(Integer[] ids) {
		boolean isSuccess=userService.deleteAll(ids);
		if(isSuccess) {
			return Result.success("删除成功");
		}else {
			return Result.error("删除失败");
		}
	}
	
	@RequestMapping("/deleteById.action")
	@ResponseBody
	public Result deleteById(Integer id) {
		System.out.println("id: " + id);
		boolean isSuccess=userService.deleteById(id);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return Result.error("删除失败");
		}
	}
	
	@RequestMapping("/getUserInsertPage.action")
	public String getUserInsertPage() {
		return "user_insert";
	}
	
	@RequestMapping("/getUserLoginPage.action")
	public String getUserLoginPage() {
		return "user_login";
	}
	
	
	@RequestMapping("/insert")
	@ResponseBody
	public Result insert(User user) {
		System.out.println(user);
		boolean isSuccess=userService.insert(user);
		if(isSuccess) {
			return Result.success("插入成功");
		}else {
			return Result.success("插入失败");
		}
	}
	
	@RequestMapping("/login.action")
	@ResponseBody
	public Result login(String username,String password,HttpSession session) {
		System.out.println("username:"+username+",password:"+password);
 		User user= userService.login(username, password);
		if(user!=null) {
			session.setAttribute("user", user);
			return Result.success("登录成功");
		}else {
			return Result.error("登录失败");
		}
		
		
	} 
}